package com.ruoyi.user.controller.app;


import com.ruoyi.common.core.annotation.AnonymousAccess;
import com.ruoyi.common.core.annotation.NoRepeatSubmit;
import com.ruoyi.common.core.config.OssConfig;
import com.ruoyi.common.core.constant.constant.PathConstant;
import com.ruoyi.common.core.pojo.vo.BaseVO;
import com.ruoyi.common.core.pojo.vo.SingleVO;
import com.ruoyi.common.core.result.Result;
import com.ruoyi.common.redis.cache.CacheTemplate;
import com.ruoyi.user.model.pojo.request.app.SendCodeRequest;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.concurrent.TimeUnit;

@Tag(name = "App-公共模块", description = "App-公共模块")
@RestController
@RequestMapping(PathConstant.APP_PATH + "common")
public class AppCommonController {


    @Autowired
    private OssConfig ossConfig;

    @AnonymousAccess
    @Operation(description = "上传文件到oss服务器",summary = "上传文件到oss服务器")
    @PostMapping("/upload")
    public SingleVO<String> upload(@RequestParam("file") MultipartFile multipartFile) throws Exception {
        String s = ossConfig.uploadFile(multipartFile);
        return Result.single(s);
    }

    @Operation(description = "发送验证码",summary = "发送验证码")
    @PostMapping("sendCode")
    @AnonymousAccess
    @NoRepeatSubmit(1000)
    public BaseVO sendCode(@RequestBody @Valid SendCodeRequest request) {
        String cache = CacheTemplate.getCache(request.getPhone() + "user");
        Assert.isNull(cache, "请勿重复发送");
        String code = "888888";
        CacheTemplate.setCache(request.getPhone(), code, 60L, TimeUnit.SECONDS);
        return Result.success();
    }

}
